package com.zryc.shop.sms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zryc.shop.sms.entity.HomeNewProduct;
import com.zryc.shop.sms.entity.HomeRecommendProduct;
import com.zryc.shop.sms.service.IHomeRecommendProductService;
import com.zryc.shop.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 人气推荐商品表 前端控制器
 * </p>
 *
 * @author LiYe
 * @since 2023-02-13
 */
@RestController
@RequestMapping("/sms/home/recommendProduct")
@Api("热门商品推荐")
public class HomeRecommendProductController {
    @Autowired
    private IHomeRecommendProductService recommendProductService;

    @GetMapping("/list")
    @ApiOperation("分页查询")
    public ResponseResult list(@RequestParam(required = false) @ApiParam(value = "商品名称关键词") String productName,
                               @RequestParam(required = false) @ApiParam(value = "推荐状态") Integer recommendStatus,
                               @RequestParam(defaultValue = "1") @ApiParam(value = "页码", required = true) Integer pageNum,
                               @RequestParam(defaultValue = "5") @ApiParam(value = "每页显示条数", required = true) Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(productName)) {
            queryWrapper.like("product_name", productName);
        }
        if (recommendStatus!=null) {
            queryWrapper.eq("recommend_status", recommendStatus);
        }
        recommendProductService.page(page, queryWrapper);
        return new ResponseResult(200, "查询完成", page);
    }

    @PostMapping("/create")
    @ApiOperation("选择热门商品推荐")
    public ResponseResult create(@RequestBody List<HomeRecommendProduct> homeRecommendProductList) {
        QueryWrapper queryWrapper = new QueryWrapper();
        for (HomeRecommendProduct recommendProduct:homeRecommendProductList) {
            queryWrapper.clear();
            queryWrapper.eq("product_id", recommendProduct.getProductId());
            queryWrapper.eq("product_name", recommendProduct.getProductName());
            if (recommendProductService.count(queryWrapper) == 0) {
                recommendProductService.save(recommendProduct);
            }
        }
        return new ResponseResult(200, "添加成功");
    }

    @PostMapping("/update/recommendStatus")
    @ApiOperation("设置推荐/取消")
    public ResponseResult updateRecommendStatus(String ids, Integer recommendStatus) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.in("id", ids.split(","));
        updateWrapper.set("recommend_status", recommendStatus);
        recommendProductService.update(updateWrapper);
        return new ResponseResult(200, "设置成功");
    }

    @PostMapping("/delete")
    @ApiOperation("删除新品推荐")
    public ResponseResult delete(String ids) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("id", ids.split(","));
        recommendProductService.remove(queryWrapper);
        return new ResponseResult(200, "删除成功");
    }

    @PostMapping("/update/sort/{id}")
    @ApiOperation("设置推荐排序值")
    public ResponseResult updateSort(@PathVariable Long id, @RequestParam Integer sort) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id);
        updateWrapper.set("sort", sort);
        recommendProductService.update(updateWrapper);
        return new ResponseResult(200, "设置成功");
    }
}
